﻿<#
.SYNOPSIS
构建 hiredis 库的 PowerShell 脚本

.DESCRIPTION
该脚本执行以下操作：
1. 解压 hiredis-master.zip（如果目录不存在）
2. 创建必要的输出目录结构
3. 使用 CMake 构建 x64/Debug、x64/Release、Win32/Debug、Win32/Release 版本
4. 将生成的文件复制到相应的输出目录
#>

# 设置错误处理
$ErrorActionPreference = "Stop"

# 检查并解压 hiredis
Write-Host "检查并解压 hiredis..."
if (-not (Test-Path "hiredis-master")) {
    Write-Host "正在解压 hiredis-master.zip..."
    Expand-Archive -Path "hiredis-master.zip" -DestinationPath "."
}

Write-Host "进入 hiredis 目录..."
# 进入 hiredis 目录
Set-Location "hiredis-master"

Write-Host "创建输出目录结构..."
# 创建输出目录结构
$directories = @(
    "..\lib",
    "..\lib\x64",
    "..\lib\Win32",
    "..\lib\x64\Debug",
    "..\lib\x64\Release",
    "..\lib\Win32\Debug",
    "..\lib\Win32\Release",
    "..\include",
    "..\include\hiredis",
    "..\include\hiredis\adapters"
)

foreach ($dir in $directories) {
    if (-not (Test-Path $dir)) {
        New-Item -ItemType Directory -Path $dir | Out-Null
    }
}

Write-Host "清理头文件..."
# 清理头文件
if (Test-Path "..\include\hiredis\*") {
    Remove-Item "..\include\hiredis\*" -Recurse -Force
}


# 构建 x64 Debug 版本
Write-Host "构建 x64 Debug 版本..."
$buildDir = "build64d"
New-Item -ItemType Directory -Path "$buildDir" -ErrorAction SilentlyContinue | Out-Null
Set-Location $buildDir

$stagingDir = ".\dist"
& cmake -G "Visual Studio 17 2022" -A x64 -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX="$stagingDir" ..
& cmake --build . --config Debug
& cmake --install . --config Debug

Copy-Item ".\dist\lib\*.lib" "..\..\lib\x64\Debug" -Force
Copy-Item ".\dist\bin\*.dll" "..\..\lib\x64\Debug" -Force
robocopy ".\dist\include\hiredis" "..\..\include\hiredis" /E /COPYALL /R:1 /W:1 /NFL /NDL | Out-Null

Set-Location ".."

# 构建 x64 Release 版本
Write-Host "构建 x64 Release 版本..."
$buildDir = "build64r"
New-Item -ItemType Directory -Path "$buildDir" -ErrorAction SilentlyContinue | Out-Null
Set-Location $buildDir

$stagingDir = ".\dist"
& cmake -G "Visual Studio 17 2022" -A x64 -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX="$stagingDir" ..
& cmake --build . --config Release
& cmake --install . --config Release

Copy-Item ".\dist\lib\*.lib" "..\..\lib\x64\Release" -Force
Copy-Item ".\dist\bin\*.dll" "..\..\lib\x64\Release" -Force

Set-Location ".."

# 构建 Win32 Debug 版本
Write-Host "构建 Win32 Debug 版本..."
$buildDir = "build32d"
New-Item -ItemType Directory -Path "$buildDir" -ErrorAction SilentlyContinue | Out-Null
Set-Location $buildDir

$stagingDir = ".\dist"
& cmake -G "Visual Studio 17 2022" -A Win32 -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX="$stagingDir" ..
& cmake --build . --config Debug
& cmake --install . --config Debug

Copy-Item ".\dist\lib\*.lib" "..\..\lib\Win32\Debug" -Force
Copy-Item ".\dist\bin\*.dll" "..\..\lib\Win32\Debug" -Force

Set-Location ".."

# 构建 Win32 Release 版本
Write-Host "构建 Win32 Release 版本..."
$buildDir = "build32r"
New-Item -ItemType Directory -Path "$buildDir" -ErrorAction SilentlyContinue | Out-Null
Set-Location $buildDir

$stagingDir = ".\dist"
& cmake -G "Visual Studio 17 2022" -A Win32 -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX="$stagingDir" ..
& cmake --build . --config Release
& cmake --install . --config Release

Copy-Item ".\dist\lib\*.lib" "..\..\lib\Win32\Release" -Force
Copy-Item ".\dist\bin\*.dll" "..\..\lib\Win32\Release" -Force

Set-Location ".."

Write-Host "构建完成！"